package org.maven.social;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import twitter4j.RateLimitStatus;
import twitter4j.Twitter;
import twitter4j.TwitterException;

/**
 * Goal which touches a timestamp file.
 * 
 * @requiresOnline true
 * @goal social
 * 
 */
public class SocialMojo extends AbstractMojo {

	/**
	 * Username of the social networking medium
	 * 
	 * @parameter expression="${social.username}"
	 */
	private String username;

	/**
	 * Password of the Social networking medium
	 * 
	 * @parameter expression="${social.password}"
	 */
	private String password;

	/**
	 * Message to be send to Social Network
	 * 
	 * @parameter expression="${social.message}"
	 */
	private String message;

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		Twitter twitter = new Twitter(username, password);
		twitter.setSource("Maven");
		try {
			RateLimitStatus rateLimitStatus = twitter.rateLimitStatus();
			if (rateLimitStatus.getRateLimitRemaining() == 0) {
				throw new RuntimeException("The hourly limit of tweets has been reached.You cannot send in more tweets");
			}
			twitter.updateStatus(message);
		} catch (TwitterException e) {
			throw new MojoExecutionException("Twitter update status failed "
					+ e.getMessage(), e);
		}
	}

}
